package com.infusionesuci2Pro;

import org.json.JSONException;
import org.json.JSONObject;

import com.infusionesuci2Pro.R;
import com.infusionesuci2Pro.R.drawable;
import com.infusionesuci2Pro.R.id;
import com.infusionesuci2Pro.R.layout;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.widget.RemoteViews;

public class PushPersonalized extends BroadcastReceiver {

	private static final String TAG = "PushPersonalized";
	@Override
	public void onReceive(Context context, Intent intent) {
		try {

            JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
            String title=json.getString("titulo");
            String message=json.getString("texto");
            String url=json.getString("url");
            NotificationManager mManager =(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            
            Intent resultIntent = new Intent(context, Notificacion.class);
   	     	resultIntent.putExtra("message", message);
   	     	resultIntent.putExtra("title", title);
   	     	resultIntent.putExtra("url", url);
            
   	     	Notification notification = new Notification(R.drawable.logo, title, System.currentTimeMillis());
            notification.flags |= Notification.FLAG_AUTO_CANCEL;

            RemoteViews contentView = new RemoteViews("com.infusionesuci2Pro",R.layout.custom_notifications);
            contentView.setImageViewResource(R.id.logo2, R.drawable.logo);
            contentView.setTextViewText(R.id.description, message);
            notification.contentView = contentView;
            notification.contentIntent = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_CANCEL_CURRENT);
            long[] vibrate = {100,100,200,300};
    	    notification.vibrate = vibrate;
    	    Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    	    notification.sound = defaultSound;
            PowerManager pm = (PowerManager) 
   		    context.getSystemService(Context.POWER_SERVICE);
            WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
	        wl.acquire(15000);
	        mManager.notify(0, notification);
            
        } catch (JSONException e) {
            Log.d(TAG, "JSONException: " + e.getMessage());
        } 
		
		
	}
	
	

}
